package com.csc.dataaccess.util;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;


public class MtlBatchHibernateUtil{
	/**
	 * Declaring session factory.
	 */
	private static SessionFactory sessionFactory = null;

	/**
	 * Method used to get Default session.
	 * 
	 * @return Session
	 */
	static {

		try {
			/**
			 * read the hibernate hibernatecfg.xml and prepare hibernate to use.
			 */
			sessionFactory = new Configuration().configure("/com/csc/mtlbatch/hbm/conf/hibernate.cfg.xml").buildSessionFactory();
		}catch (Exception e){
			e.printStackTrace();
		}
//		catch (HibernateException ex) {
//			ex.printStackTrace();
//		}
	}

	/**
	 * Get session object
	 * 
	 * @return session
	 */
	public Session getSession() {

		/**
		 * Declaring session.
		 */
		Session session = null;
		try {

		session = sessionFactory.openSession();

		}catch (HibernateException e){
			e.printStackTrace();
		}
		return session;
	}

	/**
	 * Method used to close statement.
	 * 
	 * @param statement
	 */
	public void closeResource(Statement statement) {

		try {
			if (null != statement) {
				statement.close();
			}
		} catch (SQLException sqlException) {
		}
	}

	/**
	 * Method used to close statement.
	 * 
	 * @param statement
	 */
	public void closeResource(ResultSet resultSet) {

		try {
			if (null != resultSet) {
				resultSet.close();
			}
		} catch (SQLException sqlException) {
			sqlException.printStackTrace();
		}
	}

	/**
	 * Method to close session.
	 */
	public void closeSession(Session session) {

		try {
			if (null != session) {
				session.close();
			}
		} catch (HibernateException ex) {
			ex.printStackTrace();
		}

	}

}
